#include "ExchangeRepoInfo.h"

class CExchangeRepoInfo_Impl {
 public:
  CExchangeRepoInfo_Impl() {}
  void clear() {
    std::lock_guard<std::mutex> locker(m_mutex);
    m_map.clear();
  }

  void add(CExchangeRepoUnit& one) {
    std::lock_guard<std::mutex> locker(m_mutex);
    if (strlen(one.ID) <= 0) return;
    m_map[one.ID] = one;
  }

  int getSize() {
    std::lock_guard<std::mutex> locker(m_mutex);
    return (int)m_map.size();
  }

  const CExchangeRepoUnit* getItem(const char* strID) {
    if (!strID || strlen(strID) <= 0) return nullptr;

    std::lock_guard<std::mutex> locker(m_mutex);
    std::map<std::string, CExchangeRepoUnit>::iterator itor = m_map.find(strID);
    if (itor == m_map.end()) return NULL;
    return &(itor->second);
  }

  std::mutex* getMutex() { return &m_mutex; }
  std::map<std::string, CExchangeRepoUnit>* getData() { return &m_map; }

 private:
  std::map<std::string, CExchangeRepoUnit> m_map;
  std::mutex m_mutex;
};

CExchangeRepoInfo::CExchangeRepoInfo() { m_impl = new CExchangeRepoInfo_Impl; }

CExchangeRepoInfo::~CExchangeRepoInfo() {
  Clear();
  if (m_impl) delete m_impl;
}

void CExchangeRepoInfo::Clear() {
  if (m_impl) m_impl->clear();
}

void CExchangeRepoInfo::Add(CExchangeRepoUnit& one) {
  if (m_impl) m_impl->add(one);
}

int CExchangeRepoInfo::GetSize() {
  if (!m_impl) return 0;
  return m_impl->getSize();
}

CExchangeRepoInfo& CExchangeRepoInfo::instance() {
  static CExchangeRepoInfo s_cri;
  return s_cri;
}

const CExchangeRepoUnit* CExchangeRepoInfo::GetItem(const char* strID) {
  if (!m_impl) return nullptr;
  return m_impl->getItem(strID);
}

std::mutex* CExchangeRepoInfo::GetMutex() {
  if (!m_impl) return nullptr;
  return m_impl->getMutex();
}

std::map<std::string, CExchangeRepoUnit>* CExchangeRepoInfo::GetData() {
  if (!m_impl) return nullptr;
  return m_impl->getData();
}

void CExchangeRepoInfo::DumpStatus(int mode) const {}
